package com.zgh.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zgh.domain.Bbs;
import com.zgh.domain.BbsComment;
import com.zgh.domain.User;
import com.zgh.mapper.BbsCommentMapper;
import com.zgh.mapper.BbsMapper;
import com.zgh.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class BbsCommentService {
    @Autowired
    private BbsCommentMapper bbsCommentMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private BbsMapper bbsMapper;

    /**
     * 发评论
     * @param comment 评论内容
     * @param openId  openId
     * @param bbsId 论坛id
     * @return i
     */
    public int sendComment(String comment, String openId, String bbsId) {
        //根据openId查询用户
        User user = userService.findByOpenId(openId);
        BbsComment bbsComment = new BbsComment();
        bbsComment.setBbsId(Integer.parseInt(bbsId));
        bbsComment.setUserId(user.getId());
        bbsComment.setComment(comment);
        bbsComment.setFid(0);
        bbsComment.setThumbCount(0);
        bbsComment.setCommentTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        //插入数据
        int count = bbsCommentMapper.insert(bbsComment);
        //查询评论数
        int comments = bbsMapper.selectComments(bbsId);
        //更新评论数
        bbsMapper.updateComments(comments+1,bbsId);
        return count;
    }

    /**
     * 点赞
     * @param id 评论id
     * @param count 点赞数
     */
    public void thumb(String id, String count) {
        UpdateWrapper<BbsComment> bbsCommentUpdateWrapper = new UpdateWrapper<>();
        BbsComment bbsComment = bbsCommentMapper.selectById(id);
        System.out.println(bbsComment);
        bbsCommentUpdateWrapper.eq("id",id).set("thumbCount",count);
        bbsCommentMapper.update(bbsComment,bbsCommentUpdateWrapper);
    }
}
